﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Service;
using S4Store.Models.CarKpi;
using S4Store.Domain.CarKpi;
using S4Store.Domain;

namespace S4Store.Controllers
{
    public class CarKpiController : ReportBaseController
    {
        public CarKpiService CarKpiService { get; set; }
        public EmployeeService SellerService { get; set; }
        public CarService CarService { get; set; }

        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            InjectReportModel(model);
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                var data = CarKpiService.GetCarKpiDayDataQuery().Where(x => x.StoreId == Store.Id).ToList();
                model.MonthDayData = data;
                return View(model);
            }
        }

        public ActionResult MonthTd(int y,int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                var config = CarKpiService.GetCarKpiMonthConfig(y, m, Store.Id);
                if (config == null )
                {
                    config = new Domain.CarKpi.CarKpiMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
            
        }

        public ActionResult InputCarKpiMonthConfig(int y,int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                var config = CarKpiService.GetCarKpiMonthConfig(y, m, Store.Id);
                if (config == null )
                {
                    config = new Domain.CarKpi.CarKpiMonthConfig();
                    config.StoreId = Store.Id;
                    config.Month = new DateTime(y, m, 1);
                }
                return View(config);
            }
        }

        public JsonResult ModifyBackInputCarKpiMonthConfig(S4Store.Domain.CarKpi.CarKpiMonthConfig config, List<CarModelMonthConfig> carmodel, bool isconfirm = false)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                var dbconfig = CarKpiService.GetCarKpiMonthConfigQuery().FirstOrDefault(x => x.Id == config.Id);
                if (dbconfig == null )
                {
                    dbconfig = new Domain.CarKpi.CarKpiMonthConfig();
                }
                if (isconfirm)
                {
                    dbconfig.IsConfirmed = true;
                }
                if (CurrentUser.Position == Position.SystemManager)
                {
                    dbconfig.StoreId = Store.Id;
                }
                else
                {
                    dbconfig.StoreId = CurrentUser.StoreId.Value;
                }
                dbconfig.Month = config.Month;
                dbconfig.LastMonthAvgvr = config.LastMonthAvgvr;
                dbconfig.ExtendTarget = config.ExtendTarget;
                dbconfig.FinancialTarget = config.FinancialTarget;
                dbconfig.InsureTarget = config.InsureTarget;
                dbconfig.LastMonthReserveDealConversionRate = config.LastMonthReserveDealConversionRate;
                dbconfig.OrderQuantityTarget = config.OrderQuantityTarget;
                dbconfig.ReplacementTarget = config.ReplacementTarget;
                dbconfig.PakeageSaleRateTagrt = config.PakeageSaleRateTagrt;
                dbconfig.DecorateSaleRateTagrt = config.DecorateSaleRateTagrt;
                dbconfig.DecorateAvgTagrt = config.DecorateAvgTagrt;
                dbconfig.PakeageDesignTarget = config.PakeageDesignTarget;
                dbconfig.InsuranceAvgTagrt = config.InsuranceAvgTagrt;
                dbconfig.TCJH = config.TCJH;
                dbconfig.InventoryIndexTarget = config.InventoryIndexTarget;
                CarKpiService.SubmitCarKpiMonthConfig(dbconfig);

                if (carmodel == null)
                {
                    carmodel = new List<CarModelMonthConfig>();
                }
                var list = new List<CarModelMonthConfig>();
                foreach (var sel in carmodel)
                {
                    sel.Month = config.Month;
                    if (CurrentUser.Position == Position.SystemManager)
                    {
                        sel.StoreId = Store.Id;
                    }
                    else
                    {
                        sel.StoreId = CurrentUser.StoreId.Value;
                    }
                    if (isconfirm)
                    {
                        sel.IsConfirmed = true;
                    }
                    if(sel.Enble)
                    {
                        list.Add(sel);
                    }
                }
                CarKpiService.SubmitCarModelMonthConfig(config.Month.Year, config.Month.Month, Store.Id, list);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }   
        }

        public ActionResult CarModelList(int y, int m)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                t.Inject(CarKpiService);
                CarModelListModel model = new CarModelListModel();
                var brands = CarService.GetEnableCarBrand(Store.PinPaiId);
                model.CarBrands = brands.ToList();
                model.CarModelMonthConfigs = CarKpiService.GetCarModelMonthConfigs(y, m, Store.Id).ToList();
                return View(model);
            }
        }

        public ActionResult InputCarKpiDayData(int y, int m,int d)
        {
            InputCarKpiDayDataModel model = new InputCarKpiDayDataModel();
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                t.Inject(CarService);
                var brands = CarService.GetEnableCarBrand().ToList();
                model.CarBrands = brands;
                model.Date = new DateTime(y, m, d);
                model.DayDatas = CarKpiService.GetCarKpiDayDatas(y, m, d, Store.Id).ToList();
                model.MonthConfigs = CarKpiService.GetCarModelMonthConfigs(y, m, Store.Id).ToList();
                model.IsConfirmed = false;
                if(model.DayDatas.Any(x=>x.IsConfirmed))
                {
                    model.IsConfirmed = true;
                }
                return View(model);
            }
        }

        public ActionResult ModifyBackInputCarKpiDayData(List<CarKpiDayData> rows,int y, int m,int d, bool isconfirm=false)
        {
            foreach (var row in rows)
            {
                if (isconfirm)
                {
                    row.IsConfirmed = true;
                }
                else
                {
                    row.IsConfirmed = false;
                }
                row.StoreId = Store.Id;
            }
            using (var t = new Transtion())
            {
                t.Inject(CarKpiService);
                CarKpiService.SubmitCarKpiDayDatas(y, m, d, Store.Id, rows);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }
    }
}
